﻿using HotelManagerComm.CommHelper;
using HotelManagerDAL.Helper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotelManagerModel.HotelModel;

namespace HotelManagerDAL.HotelDAL
{
    /// <summary>
    /// 管理员登录
    /// </summary>
    public class ManagerDAL
    {
        #region 返回 HotelManager 对象

        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        public static HotelManager GetHotelManager(string loginId, string loginPwd)
        {
            StringBuilder sb = new StringBuilder();
            BookCommandType commandType = BookCommandType.Text;
            sb.Append("SELECT Id,LoginId,LoginPwd From Hotel_Manager ");
            sb.Append(" WHERE LoginId=@LoginId AND LoginPwd=@LoginPwd");
            SqlParameter[] paras =
            {
                new SqlParameter ("@LoginId",loginId ),
                new SqlParameter ("@LoginPwd",loginPwd )
            };
            DataTable dt = DBHelper.GetDataTable(sb.ToString(), commandType, paras);
            HotelManager manager = default;
            foreach (DataRow dr in dt.Rows)
            {
                manager = new HotelManager
                {
                    Id = dr["Id"].ChangeInt(),
                    LoginId = dr["LoginId"].ToString(),
                    LoginPwd = dr["LoginPwd"].ToString()
                };
            }
            return manager;
        }

        #endregion

        #region 更新管理员密码

        /// <summary>
        /// 更新管理员密码
        /// </summary>
        /// <param name="manager">管理员对象</param>
        /// <returns>大于0：修改成功 小于0：修改失败</returns>
        public static int UpdatePassById(HotelManager manager)
        {
            StringBuilder sb = new StringBuilder();
            BookCommandType commandType = BookCommandType.Text;
            sb.Append("UPDATE Hotel_Manager SET LoginPwd=@LoginPwd WHERE Id=@Id");
            SqlParameter[] parameters = {
                new SqlParameter ("@LoginPwd",manager.LoginPwd ),
                new SqlParameter ("@Id",manager .Id )
            };
            try
            {
                return DBHelper.ExecuteNoneQuery(sb.ToString(), commandType, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

    }
}
